/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.anniversary.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.anniversary.domain.BasicInfo;
import me.zhengjie.modules.anniversary.repository.BasicInfoRepository;
import me.zhengjie.modules.anniversary.service.BasicInfoService;
import me.zhengjie.modules.anniversary.service.dto.BasicInfoDto;
import me.zhengjie.modules.anniversary.service.dto.BasicInfoQueryCriteria;
import me.zhengjie.modules.anniversary.service.mapstruct.BasicInfoMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Jiayin Wei
* @date 2020-07-21
**/
@Service
@RequiredArgsConstructor
public class BasicInfoServiceImpl implements BasicInfoService {

    private final BasicInfoRepository basicInfoRepository;
    private final BasicInfoMapper basicInfoMapper;

    @Override
    public Map<String,Object> queryAll(BasicInfoQueryCriteria criteria, Pageable pageable){
        Page<BasicInfo> page = basicInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(basicInfoMapper::toDto));
    }

    @Override
    public List<BasicInfoDto> queryAll(BasicInfoQueryCriteria criteria){
        return basicInfoMapper.toDto(basicInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BasicInfoDto findById(Long id) {
        BasicInfo basicInfo = basicInfoRepository.findById(id).orElseGet(BasicInfo::new);
        ValidationUtil.isNull(basicInfo.getId(),"BasicInfo","id",id);
        return basicInfoMapper.toDto(basicInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BasicInfoDto create(BasicInfo resources) {
        return basicInfoMapper.toDto(basicInfoRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BasicInfo resources) {
        BasicInfo basicInfo = basicInfoRepository.findById(resources.getId()).orElseGet(BasicInfo::new);
        ValidationUtil.isNull( basicInfo.getId(),"BasicInfo","id",resources.getId());
        basicInfo.copy(resources);
        basicInfoRepository.save(basicInfo);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            basicInfoRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<BasicInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BasicInfoDto basicInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", basicInfo.getName());
            map.put("类型（0：民族、1：政治面貌、2：学历层次）", basicInfo.getType());
            map.put("创建者名称", basicInfo.getCreateBy());
            map.put("创建时间", basicInfo.getCreateTime());
            map.put("修改者名称", basicInfo.getUpdatedBy());
            map.put("修改时间", basicInfo.getUpdateTime());
            map.put("排序", basicInfo.getSort());
            map.put("备注", basicInfo.getRemark());
            map.put("是否启用", basicInfo.getIsEnable());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}